<?php
require_once 'data_form'.EXT;

class prot_death extends data_form{

	function __construct(){
		parent::__construct();
	}
	
	function index()
	{
		$this->data['module_title']	=	'Protection Against Death';
		$data	=	null;
		$this->load->model(array('pade_income_model','pade_e_loans_model','pade_e_dependants_model','explanation_model'));
		$this->load->model(array('existing_coverage_endowment_model','existing_coverage_term_model','existing_coverage_ilp_model','existing_coverage_wl_model','common_model'));
		$row	=	null;
		$this->load->helper('custom_string');
		$this->data['endowments']	=	$this->existing_coverage_endowment_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['terms']	=	$this->existing_coverage_term_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['ilps']	=	$this->existing_coverage_ilp_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['wls']	=	$this->existing_coverage_wl_model->get_many_by('customer_id',$_SESSION["customer"]["id"]);
		
		if(isset($_GET['act']))
		{
			if($_GET['act'] == 'Income')
			{
				$row	=	$this->pade_income_model->get_by(array('customer_id' => $_SESSION['customer']['id']));
				
				if($_SERVER["REQUEST_METHOD"] == "GET" && $row)
				{
					unset($_SESSION['data']);
					$data	=	array(
						'customer_id'	=>	$row->customer_id,
						'yearly_income'	=>	$row->yearly_income,
						'percent_of_protection'	=>	$row->percent_of_protection,
						'number_years'	=>	$row->number_years,
						'explanation'	=>	$row->explanation
					);
					$_SESSION['data']	=	$data;
					
				}
				
				if($_SERVER["REQUEST_METHOD"] == "POST")
				{
					$submit	=	$this->input->post('submit');
					$data	=	array(
							'customer_id'	=>	$_SESSION['customer']['id'],
							'yearly_income'	=>	$_POST['textfield5'],
							'percent_of_protection'	=>	$_POST['textfield2'],
							'number_years'	=>	$_POST['textfield6'],
							'explanation'	=>	($row ? $row->explanation : '')
							
						);
					if(isset($_POST['btnCalculate']) && $_POST['btnCalculate'] =='Calculate')
					{
						
						$_SESSION['data']	=	$data;
					}//case caculate
					else
					{
						$_SESSION['data']['explanation']	=	$_POST['Explanation'];
						$data['explanation']	=	$_POST['Explanation'];
						
						if($row)
						{
							$this->pade_income_model->update($row->id,$data);
						}//case update data
						else
						{
							$this->pade_income_model->insert($data);
						}//case insert data
						
						$this->pade_e_loans_model->delete_by('customer_id', $_SESSION['customer']['id']);
						$this->pade_e_dependants_model->delete_by('customer_id', $_SESSION['customer']['id']);
						$this->explanation_model->delete_by('customer_id', $_SESSION['customer']['id']);
						
						redirect(current_link());
					}//case submit
				}//case method = post
			}//case act = Income
			
			if($_GET['act'] == 'Expense')
			{
				//unset($_SESSION['data']);
				$loans			=	$this->pade_e_loans_model->get_many_by(array('customer_id' => $_SESSION['customer']['id']));
				$dependants		=	$this->pade_e_dependants_model->get_many_by(array('customer_id' => $_SESSION['customer']['id']));
				$exp			=	$this->explanation_model->get_by(array('data_form_id' => 2,'customer_id' => $_SESSION['customer']['id']));
				
				//print_r($exp);
				if($_SERVER["REQUEST_METHOD"] == "GET" && $loans && $dependants)
				{
					unset($_SESSION['data']);
					foreach($loans as $k => $v){
						$temp	=	array(
							'customer_id'	=>	$v->customer_id,
							'duration'		=>	$v->duration,
							'amount_per_month'	=>	$v->amount_per_month,
							'id'			=>	$v->id
						);
						$data['loans'][]	=	$temp;
					}
					
					foreach($dependants as $k => $v){
						$temp	=	array(
							'customer_id'	=>	$v->customer_id,
							'duration'		=>	$v->duration,
							'amount_per_month'	=>	$v->amount_per_month,
							'id'			=>	$v->id
						);
						$data['dependants'][]	=	$temp;
					}
					
					if($exp)
					{
						$_SESSION['data']['explanation']	=	$exp->explanation;
						$data["explanation"]	=	$exp->explanation;
					}
					$_SESSION['data']	=	$data;
				}//case method = get
				
				if($_SERVER["REQUEST_METHOD"] == "POST")
				{
					$submit	=	$this->input->post('submit');
					
					$loans_duration	=	$this->input->post('loans_duration');
						$loans_amount	=	$this->input->post('loans_amount');
						$loans_id		=	$this->input->post('loans_id');
						
						foreach($loans_duration as $k => $v){
							$temp	=	array(
								'customer_id'	=>	$_SESSION['customer']['id'],
								'duration'		=>	$v,
								'amount_per_month'	=>	$loans_amount[$k],
								'id'			=>	$loans_id[$k]
							);
							
							$data['loans'][]	=	$temp;
						}
						
						$dep_duration	=	$this->input->post('dep_duration');
						$dep_amount		=	$this->input->post('dep_amount');
						$dep_id		=	$this->input->post('dep_id');
						
						foreach($dep_duration as $k => $v){
							$temp	=	array(
								'customer_id'	=>	$_SESSION['customer']['id'],
								'duration'		=>	$v,
								'amount_per_month'	=>	$dep_amount[$k],
								'id'			=>	$dep_id[$k]
							);
							
							$data['dependants'][]	=	$temp;
						}
					//echo $submit;
					if(isset($_POST['btnCalculate']) && $_POST['btnCalculate']=='Calculate')
					{
						
						$_SESSION['data']	=	$data;
						
						if($exp){
							$_SESSION['data']['explanation']	=	$exp->explanation;
						}
						else
						{
							$_SESSION['data']['explanation']	=	'';
						}
						
						
					}//case caculate
					else
					{
						//die($this->input->post('textfield3'));
						$_SESSION['data']['explanation']	=	$this->input->post('textfield3');
						//$data	=	$_SESSION['data'];
						$data['explanation']	=	$this->input->post('Explanation');
						
						if($exp){
							$this->explanation_model->update($exp->id,array('explanation' => $data['explanation'],'data_form_id' => 2));
						}
						else 
						{
							$this->explanation_model->insert(array('explanation' => $data['explanation'],'customer_id' => $_SESSION['customer']['id'],'data_form_id' => 2));
						}
						
						$loans	=	$data['loans'];
						$dep	=	$data['dependants'];
						
						$arr_loans_id = null;
						foreach($loans as $k => $v){
							if(isset($v['id']) && $v['id'] != 0){
								$id	=	$v['id'];
								unset($v['id']);
								$this->pade_e_loans_model->update($id,$v);
							}//update
							else{
								unset($v['id']);
								$id	=	$this->pade_e_loans_model->insert($v);
							}//insert
							$arr_loans_id[] =$id;
							$_SESSION['data']['loans'][$k]['id']	=	$id;
						}
						
						//
						if($arr_loans_id){
							$arr_loans_del	=	$this->db->select()->from('pade_e_loans')->where_not_in('id',$arr_loans_id)->where('customer_id',$_SESSION['customer']['id'])->get()->result();
							foreach($arr_loans_del as $v){
								$this->pade_e_loans_model->delete($v->id);
							}
						}
						//
						
						$arr_dep_id	=	null;
						foreach($dep as $k => $v){
							if(isset($v['id']) && $v['id'] != 0){
								$id	=	$v['id'];
								unset($v['id']);
								$this->pade_e_dependants_model->update($id,$v);
							}//update
							else{
								$id	=	$this->pade_e_dependants_model->insert($v);
							}//insert
							$_SESSION['data']['dependants'][$k]['id']	=	$id;
							$arr_dep_id[]	=	$id;
						}
						
						if($arr_dep_id){
							$arr_dep_del	=	$this->db->select()->from('pade_e_dependants')->where_not_in('id',$arr_dep_id)->where('customer_id',$_SESSION['customer']['id'])->get()->result();
							foreach($arr_dep_del as $v){
								$this->pade_e_dependants_model->delete($v->id);
							}
						}
						
						$this->pade_income_model->delete_by('customer_id',$_SESSION['customer']['id']);
						
						//echo '<script>window.close();</script>';
						redirect(current_link());
					}//case submit
					
				}//case method = post
				
			}//case act = ''Expense
		}
		
		$this->load->model('current_holding_model');
		$current    =    $this->db->select()->from('current_holdings')->where('customer_id',$_SESSION["customer"]["id"])->get()->row_array();//$this->current_holding_model->get_by('customer_id',$_SESSION["customer"]["id"]);
		$this->data['current']	=	$current;
		
		$this->data['row']	=	(isset($_SESSION['data']) ? $_SESSION['data'] : null);
		$this->load->view("prot_death/index", $this->data);
	}

}